<?php
// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2018~2018 GoMaoMao [ http://www.gomaomao.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.gomaomao.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | 联系方式：GoMaoMao <vip.vvm@qq.com>
// +----------------------------------------------------------------------
// | github开源项目：https://gitee.com/gomaomao.com/ThinkAdmin
// +----------------------------------------------------------------------
namespace app\miniprogram\controller\api;

use controller\BasicApi;
use service\DataService;
use service\HttpService;
use think\facade\Log;
use think\Db;

class Api extends BasicApi
{
	function log()
	{
		$_param            = $this->param;
		$data              = $_param['systemInfo'];
		$data['open_id']   = $_param['open_id'];
		$data['create_at'] = time();
		Db::name('logs')->strict(false)->insert($data);
//		Log::log('data', $data);
		$this->_result([], 0, 'ok');
	}

	public function jsCode()
	{
		if ($this->request->isPost()) {
			$_param = $this->param;
			if (empty($_param['website'])) {
				$this->_result([], 250, '错误的请求');
			}
			$_wid       = $_param['website'];
			$_website   = DataService::cache('website', $_wid, false);
			$_appId     = $_website['appid'];
			$_appSecret = $_website['appsecret'];
			$_code      = $_param['code'];
			$url        = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $_appId . '&secret=' . $_appSecret . '&js_code=' . $_code . '&grant_type=authorization_code';
			$json       = HttpService::get($url);
			$json       = json_decode($json);
			$this->_result($json);
		} else {
			$this->_result([], 250, '错误的请求');
		}
	}
}